<?php
namespace app\admin\controller\school;
use app\common\controller\Backend;
use think\Controller;
use think\Request;
use think\Lang;
use fast\Tree;
use fast\Cols;
use fast\Date;
use think\cache\Driver\File;
class Banji extends Backend{
		protected $model = null;//模型对象
		protected $columnlist = array();
		public function _initialize(){
				parent::_initialize();
				$this->model = model('Banji');
				$this->columnarry = Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/module.php','',1);
		}
		public function listforajax(){
				$lst = $this->model->order('itemid','desc')->select();
				print_r(json_encode($lst));
				exit;
		}
		public function add(){
				if ($this->request->isPost()){
						$params = $this->request->post("row/a");
						if ($params){
								$fc = new File();
								$this->modelValidate = false;
								if ($this->dataLimit){
										$params[$this->dataLimitField] = $this->auth->id;
								}
								try{
										//是否采用模型验证
										if ($this->modelValidate){
												$name = basename(str_replace('\\', '/', get_class($this->model)));
												$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
												$this->model->validate($validate);
										}
										$result = $this->model->allowField(true)->save($params);
										if ($result !== false){
												$this->success();
										}else{
												$this->error($this->model->getError());
										}
								}
								catch (\think\exception\PDOException $e){
										$this->error($e->getMessage());
								}
						}
						$this->error(__('Parameter %s can not be empty', ''));
				}else{
						$fc = new File();
						return $this->view->fetch();
				}
		}
		public function edit($ids = NULL){
				$row = $this->model->find($ids);$fc = new File();
				if (!$row){$this->error(__('No Results were found'));}
				$adminIds = $this->getDataLimitAdminIds();
				if (is_array($adminIds)){
						if (!in_array($row[$this->dataLimitField], $adminIds)){
								$this->error(__('You have no permission'));
						}
				}
				if ($this->request->isPost()){
						$params = $this->request->post("row/a");
						if ($params){
								try{
										//是否采用模型验证
										if ($this->modelValidate){
												$name = basename(str_replace('\\', '/', get_class($this->model)));
												$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : true) : $this->modelValidate;
												$row->validate($validate);
										}
										$result = $row->allowField(true)->save($params);
										if ($result !== false){$this->success();}
										else{$this->error($row->getError());}
								}
								catch (\think\exception\PDOException $e){
										$this->error($e->getMessage());
								}
						}
				$this->error(__('Parameter %s can not be empty', ''));
				}
				$this->view->assign("row", $row);
				return $this->view->fetch();
		}
		public function index(){
				$columnlist = Cols::columnread('banji');
				$flag = array();
				foreach($columnlist as $v){
						$flag[] = $v['listorder'];
				}
				array_multisort($flag, SORT_DESC, $columnlist);
				//设置过滤方法
				$this->request->filter(['strip_tags']);
				//如果发送的来源是Selectpage，则转发到Selectpage
				if ($this->request->request('pkey_name')){
						return $this->selectpage();
				}
				$searchfileds = array();
				$searchfileds['name'] = 'varcharshort';
				list($where, $sort, $order, $offset, $limit) = $this->buildparams($searchfileds);
				$order = 'desc';
				$sort = 'itemid';
				$total = $this->model
				->where($where)
				->order($sort, $order)
				->count();
				$totalpage = 1;$page = 1;
				if($limit>0){
						$totalpage = $total%$limit==0?intval($total/$limit):intval($total/$limit)+1;
						//总页数
						if($offset>0){$page = intval($offset/$limit)+1;}
				}
				$list = $this->model
				->where($where)
				->order($sort, $order)
				->limit($offset, $limit)
				->select();
				for ($i = 0; $i < count($list); $i++) {
						if(isset($list[$i]['category'])){
								$list[$i]['catname'] = $list[$i]['category']['name'];
						}
						unset($list[$i]['intro']);unset($list[$i]['content']);
						$list[$i]['updatetime'] = Date::timetodate($list[$i]['updatetime'],3);
						$list[$i]['createtime'] = Date::timetodate($list[$i]['createtime'],3);
				}
				$result = array("total" => $total, "rows" => $list);
				$this->view->assign("lists",$result['rows']);
				$this->view->assign("columnlist",$columnlist);
				$this->view->assign("total",$result['total']);
				$this->view->assign("offset",$offset);
				$this->view->assign("limit",$limit);
				$this->view->assign("totalpage",$totalpage);
				$this->view->assign("page",$page);
				return $this->view->fetch('index');
		}
}?>
